package com.aier.school.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.CityUtil;
import com.aier.common.common.Constant;
import com.aier.common.common.ImageUtil;
import com.aier.common.common.SystemConfigStatic;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolInfoDetail;
import com.aier.school.entity.SchoolPhoto;
import com.aier.school.entity.SchoolTeacherCharisma;
import com.aier.school.service.ISchoolService;
import com.aier.user.entity.Dynamic;
import com.aier.user.service.IDynamicService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Scope("prototype")
@Component("schoolBaseInfoAction")
public class SchoolBaseInfoAction extends SchoolBaseAction{

    /**
     * 学校管理-学校基本信息、校徽
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "schoolService")
    private ISchoolService schoolService;   
    @Resource(name="dynamicService")
    private IDynamicService dynamicService;
    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
    
    private School school;        //学校
    private Integer sid;        //学校id
    private String name;        //学校名称
//    private Integer cid;        //所在城市id
    private String desinfo;        //学校简介
    private String img;            //学校主图
    private String slogo;        //学校logo
    private File file;
    private String photoName; // 照片原名


    private int x;
    private int y;
    private int w;
    private int h;
    
    private Long stcid;  // 学校教师风采id
    private String charisma; // 教师风采（多个教师之间以@分割）
    private String phone;  // 学校联系电话
    private String address; // 学校地址
    private String url;  // 学校网站
    private String postcode;  // 学校邮编
    private String email;
    private String fax;
    private List<SchoolPhoto> spList; // 学校相册图片列表
    private List<SchoolTeacherCharisma> stcList; // 教师风采
    private SchoolInfoDetail schoolInfoDetail;  // 学校详细信息
    private Dynamic dynamic;  // 获取学校最新动态
    
    
//app端用
    private String schools;
    private String schoolInfoDetails;
    private String spLists;
    private String stcLists;
    private String  stcname, stcImg, stcDesinfo;//教师风采几个字段

// app end 
    
    @Override
    public String execute(){
        school = schoolService.searchSchoolBySid(sid);
        return SUCCESS;
    }    
    
    /**
     * 新版获取学校主页的信息app
     * @return
     */
    public String searchSchoolInfo() {
        // 学校风采
        spList = schoolService.searchSchoolPhotoList(sid);
        
        school = schoolService.searchSchoolBySid(sid);
        school.setAddress(CityUtil.addressDetail(school.getCid()));
    	// 教师风采
        stcList = schoolService.searchSTCList(sid);
        
        //基本信息
        schoolInfoDetail=schoolService.searchSchoolInfoBySid(sid); 
        
        s = 1;
        return SUCCESS;
    }
    
    //新版 教师风采
    public String searchTeachersLists(){
        spList = schoolService.searchSchoolPhotoList(sid);
        
        school = schoolService.searchSchoolBySid(sid);
        school.setAddress(CityUtil.addressDetail(school.getCid()));
    	// 教师风采
    	// 教师风采
        stcList = schoolService.searchSTCList(sid);
        
        s = 1;
        return SUCCESS;
    }
    
    //*新版基本信息
    public String searchschoolInfoDetail(){
    	  //基本信息
        schoolInfoDetail=schoolService.searchSchoolInfoBySid(sid); 
        s = 1;
    	return SUCCESS;
    }
    
    
    /**
     * 修改学校信息
     * @return
     */
    public String updateSchoolInfo(){
        s = 1;
        if(schoolService.countSchoolByName(name, sid)>0){
            s = 0;
            error_info = "学校名称已经存在";
        }else{
            school = new School(sid, name, null, null, desinfo, img, null, null,1);
            if(schoolService.updateSchoolInfo(school)==0){
                s = 0;
                error_info = "修改学校信息失败";
            }            
        }

        return SUCCESS;
    }
    
    /**
     * 修改学校logo(jquery版)
     * @return
     */
    public String updateSchoolLogo(){
        slogo = ImageUtil.uploadImage(file,schoollogo_upload_url, photoName,1);
        schoolService.updateSchoolLogo(sid, slogo);
        s = 1;
        return SUCCESS;
    }    
   
    /**
     * 修改学校logo(flash版)
     * 
     * @return
     */
    public String updateFlashschoolLogo() {
        slogo = ImageUtil.uploadImageAdjustable(file, schoollogo_upload_url, photoName,Constant.num_100, 
                Constant.num_100, Constant.num_200, Constant.num_200, true);
        if(schoolService.updateSchoolLogo(sid, slogo)==0){
            s = 0;
            error_info = "修改学校logo失败";
        }
        s=1;
        return SUCCESS;
    }
    
    // 剪切校徽
    public String cutImg() {
        s = ImageUtil.cutImg(SystemConfigStatic.schoollogo_upload_url, slogo, x, y, w, h);        
        if(schoolService.updateSchoolLogo(sid, slogo)==0){
            s = 0;
            error_info = "修改学校logo失败";
        }
        return SUCCESS;
    }
    
    
    // 读取校徽
    public String serachAvatar() {
        School sc = schoolService.searchSchoolBySid(sid);
        if (null != sc) {
            slogo = sc.getSlogo();
        }
        return SUCCESS;
    }
    
    
    // 新版
    /**
     * 修改学校简介
     * @return
     */
    public String updateSchoolDesinfo() {
        s = schoolService.updateSchoolDesinfo(sid, desinfo);
        return SUCCESS;
    }
    

    
    /**
     * 修改学校教师风采
     * @return
     */
    public String updateTeacherCharisma() {
        s = schoolService.updateTeacherCharisma(stcid, name, img, desinfo);
        return SUCCESS;
    }
    
    /**
     * 删除学校教师风采
     * @return
     */
    public String deleteTeacherCharisma() {
        s = schoolService.deleteTeacherCharisma(stcid);
        return SUCCESS;
    }
    
    /**
     * 新增学校教师风采
     * @return
     */
    public String addTeacherCharisma() {
        s = schoolService.insertTeacherCharisma(sid,charisma);
        return SUCCESS;
    }
    
    /**
     * 修改学校信息
     * @return
     */
    public String updateSchoolInfoDetail() {
        s = schoolService.updateSchoolInfo(sid, phone, address, url, postcode,email,fax);
        return SUCCESS;
    }
    
    /**
     * 修改学校头像
     * @return
     */
    public String updateSchoollogo() {
    	slogo = ImageUtil.uploadImage(file,schoollogo_upload_url, photoName, 1);
        s = schoolService.updateSchoolLogo(sid, slogo);
        return SUCCESS;
    }
    
    
    /**
     * app修改学校信息
     * @return
     */
    public String updateSchoolDesinfoApp(){
    	Gson gson=new Gson();
			school = gson.fromJson(schools, new TypeToken<School>() {
			}.getType());
			schoolService.updateSchoolDesinfo(school.getSid(), school.getDesinfo());
			
			schoolInfoDetail = gson.fromJson(schoolInfoDetails, new TypeToken<SchoolInfoDetail>() {
			}.getType());
			s=schoolService.updateSchoolInfo(school.getSid(), schoolInfoDetail.getPhone(), schoolInfoDetail.getAddress(), schoolInfoDetail.getUrl(), schoolInfoDetail.getPostcode(),schoolInfoDetail.getEmail(),schoolInfoDetail.getFax());
			
    	return SUCCESS;
    }
    
    //更新教师风采
    public String  updateTeacherCharismal(){
    	s=schoolService.updateTeacherCharisma(stcid, stcname, stcImg, stcDesinfo);
    	return SUCCESS;
    }
    //新增教师风采
    public String  addTeacherCharismal(){
    	s=schoolService.insertTeacherCharisma(sid, stcImg, stcname,stcDesinfo);
    	return SUCCESS;
    }
    
    public int getS() {
        return s;
    }
    public String getError_info() {
        return error_info;
    }
    public School getSchool() {
        return school;
    }
    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }
    public void setSid(Integer sid) {
        this.sid = sid;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setDesinfo(String desinfo) {
        this.desinfo = desinfo;
    }
    public void setImg(String img) {
        this.img = img;
    }
    public void setSlogo(String slogo) {
        this.slogo = slogo;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public void setPhotoName(String photoName) {
        this.photoName = photoName;
    }

    public String getSlogo() {
        return slogo;
    }
    

    public Integer getSid() {
        return sid;
    }

    public void setStcid(Long stcid) {
        this.stcid = stcid;
    }

    public void setCharisma(String charisma) {
        this.charisma = charisma;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }

    public List<SchoolPhoto> getSpList() {
        return spList;
    }

    public List<SchoolTeacherCharisma> getStcList() {
        return stcList;
    }

    public SchoolInfoDetail getSchoolInfoDetail() {
        return schoolInfoDetail;
    }

    public void setDynamicService(IDynamicService dynamicService) {
        this.dynamicService = dynamicService;
    }

    public Dynamic getDynamic() {
        return dynamic;
    }

	public void setSchools(String schools) {
		this.schools = schools;
	}

	public void setSchoolInfoDetails(String schoolInfoDetails) {
		this.schoolInfoDetails = schoolInfoDetails;
	}

	public void setSpLists(String spLists) {
		this.spLists = spLists;
	}

	public void setStcLists(String stcLists) {
		this.stcLists = stcLists;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public void setStcname(String stcname) {
		this.stcname = stcname;
	}

	public void setStcImg(String stcImg) {
		this.stcImg = stcImg;
	}

	public void setStcDesinfo(String stcDesinfo) {
		this.stcDesinfo = stcDesinfo;
	}
	
}